{% import _self as self %} {# Required to use macros in same template #}

<div class="content">
    {#
        Default is viewing your sections
        Limited grader does not have "View All" option
        If nothing to grade, Instructor will see all sections
    #}
    <div class="row" style="justify-content: space-between;margin-bottom: 5px">
    <h1>Grade Details for {{ gradeable.getTitle() }}</h1>
        <div style="display: flex;flex-wrap: wrap;">
            <ul style="justify-self: flex-end" id="details-legend" class="table-bordered">
                <li>
                    <i class="fas fa-circle grader-NULL"></i>
                    Ungraded component
                </li>
                <li>
                    <i class="fas fa-circle grader-3"></i>
                    Component graded by a limited access grader
                </li>
                <li>
                    <i class="fas fa-circle grader-1"></i>
                    Component graded by a full access grader
                </li>
                <li>
                    <i class="fas fa-circle grader-verified"></i>
                    Limited access grader verified
                </li>
                <li>
                    <i class="fas fa-circle grader-double"></i>
                    Component double-graded by a full access grader or instructor
                </li>
            </ul>

            <div style="display:flex; flex-direction: column; justify-self: flex-end;flex-wrap: wrap;margin-left: 5px">
                {% if show_all_sections_button %}
                <a class="btn btn-default"
                   href="{{ view_all_toggle_url }}">
                    {{ view_all ? "View Your Sections" : "View All" }}
                </a>
                {% endif %}
                <a style="margin-top: 10px" class="btn btn-default"
                   href="{{ order_toggle_url }}">
                    {{ sort == 'random' ? "Default Order" : "Random Order" }}
                </a>
            </div>
        </div>
    </div>
    {# Import+Export teams buttons #}
    {% if show_import_teams_button %}
        <a style="float: right;" class="btn btn-primary" href="{{ export_teams_url }}">Export Teams Members</a>
    {% endif %}
    {% if show_export_teams_button %}
        <button style="float: right;" class="btn btn-primary" onclick="importTeamForm();">Import Teams Members</button>
    {% endif %}
    {# /Import+Export teams buttons #}

	{# Randomize team button #}
	{% if (not gradeable.isGradeByRegistration()) and (show_export_teams_button or show_import_teams_button) %}
		{% if past_grade_start_date %}
			<Button style="float: left;" class="btn btn-default" onclick="randomizeRotatingGroupsButton()"> Randomly Re-Assign Teams to Rotating Sections</Button>
		{% else %}
			<a style="float: left;" class="btn btn-default" href="{{ randomize_team_rotating_sections_url }}"> Randomly Re-Assign Teams to Rotating Sections</a>
		{% endif %}
	{% endif %}

    <br />
    <br />
    {# This is a data table #}
    <table class="table table-striped table-bordered persist-area table-text-left">
        <caption />
        {# Table header #}
        <thead>
            <tr>
                {% for column in columns %}
                    {% if column.sort_type %}
                        <td width="{{ column.width }}">
                            <a href="{{ details_base_url }}?view={{ (core.getUser().getGradingRegistrationSections()|length == 0) ? 'all' : null }}&sort={{ column.sort_type }}&direction={{ sort == column.sort_type ? (direction == 'ASC' ? 'DESC' : 'ASC') : 'ASC' }}"
                            {% if sort == column.sort_type %}
                                class="active-sort"
                            {% endif %}
                            >
                                {{ column.title }}
                                {% if sort == column.sort_type %}
                                    <i class="fa {{ direction == 'ASC' ? 'fa-sort-up' : 'fa-sort-down' }}"></i>
                                {% else %}
                                    <i class="fa fa-sort"></i>
                                {% endif %}
                            </a>
                        </td>
                    {% else %}
                        <th width="{{ column.width }}">{{ column.title }}</th>
                    {% endif %}
                {% endfor %}
            </tr>
        </thead>
        {# /Table header #}
        {# Sections #}
        {% if sections|length == 0 %}
            <tbody>
                <tr class="info">
                    <td colspan="{{ 42 + gradeable.getComponents()|length }}" style="text-align: center"> No Grading To Be Done! :) </td>
                </tr>
            </tbody>
        {% else %}
            {% for section in sections %}
                {# Section heading #}
                <tbody>
                    <tr class="info">
                        <td colspan="{{ columns|length }}" style="text-align: center">
                            {% if gradeable.isGradeByRegistration() %}
                                Students Enrolled in Registration Section {{ section.title }}
                            {% else %}
                                Students Assigned to Rotating Section {{ section.title }}
                            {% endif %}
                        </td>
                    </tr>
                    <tr class="info">
                        <td colspan="{{ columns|length }}" style="text-align: center">Graders: {{ section.graders }}</td>
                    </tr>
                </tbody>
                {# /Section heading #}
                {# Student list #}
                <tbody>
                    {% for info in section.rows %}
                        {{ self.render_student(self, _context, section, info.graded_gradeable, loop.index, info, columns, team_gradeable_view_history) }}
                    {% endfor %}
                    {% for info in section.teamless_users %}
                        {{ self.render_teamless_user(self, _context, section, info.user, info.new_team_onclick, loop.index+section.rows|length, columns) }}
                    {% endfor %}
                </tbody>
                {# /Student list #}
            {% endfor %}
        {% endif %}
        {# /Sections #}
        {# Empty teams #}
        {% if gradeable.isTeamAssignment() and empty_teams|length > 0 %}
            <tr class="info">
                <td colspan="{{ columns|length }}" style="text-align: center">Empty Teams</td>
            </tr>
            <tbody>
                {% for team in empty_teams %}
                    {% set reg_section = team.getRegistrationSection() ?? "NULL" %}
                    {% set rot_section = team.getRotatingSection() ?? "NULL" %}
                    {% set display_section = gradeable.isGradeByRegistration() ? reg_section : rot_section %}
                    <tr id="empty-team-row-{{ team.getId() }}">
                        <td>
                            {{ loop.index }}
                        </td>
                        <td>
                            {{ display_section }}
                        </td>
                        <td>
                            <a onclick="{{ empty_team_info[loop.index0].team_edit_onclick }}" aria-label="Edit Team">
                                <i class="fas fa-pencil-alt"></i>
                            </a>
                        </td>
                        <td>{{ team.getId() }}</td>
                        <td colspan="{{ columns|length - 4 }}"></td>
                    </tr>
                {% endfor %}
            </tbody>
        {% endif %}
        {# /Empty teams #}
    </table>
</div>

{% macro render_student(self, context, section, graded_gradeable, index, info, columns, team_gradeable_view_history) %}
    <tr
        {% if student.accessGrading() %}
            style='background: #7bd0f7;'
        {% endif %}
    >
        {% for column in context.columns %}
            {#
                This is basically a switch statement. It calls the function we need for
                the current column and that will give us the table cell.
            #}
            {% if column.function == "active_version" %}
                {{ self.render_column_active_version(self, context, section, graded_gradeable, index, info, column) }}
            {% elseif column.function == "autograding" %}
                {{ self.render_column_autograding(self, context, section, graded_gradeable, index, info, column) }}
            {% elseif column.function == "autograding_peer" %}
                {{ self.render_column_autograding_peer(self, context, section, graded_gradeable, index, info, column) }}
            {% elseif column.function == "graded_questions" %}
                {{ self.render_column_graded_questions(self, context, section, graded_gradeable, index, info, column) }}
            {% elseif column.function == "grading" %}
                {{ self.render_column_grading(self, context, section, graded_gradeable, index, info, column) }}
            {% elseif column.function == "grading_peer" %}
                {{ self.render_column_grading_peer(self, context, section, graded_gradeable, index, info, column) }}
            {% elseif column.function == "index" %}
                {{ self.render_column_index(self, context, section, graded_gradeable, index, info, column) }}
            {% elseif column.function == "section" %}
                {{ self.render_column_section(self, context, section, graded_gradeable, index, info, column) }}
            {% elseif column.function == "team_edit" %}
                {{ self.render_column_team_edit(self, context, section, graded_gradeable, index, info.team_edit_onclick, column) }}
            {% elseif column.function == "team_id" %}
                {{ self.render_column_team_id(self, context, section, graded_gradeable, index, info, column) }}
            {% elseif column.function == "team_members" %}
                {{ self.render_column_team_members(self, context, section, graded_gradeable, index, info, column) }}
            {% elseif column.function == "total" %}
                {{ self.render_column_total(self, context, section, graded_gradeable, index, info, column) }}
            {% elseif column.function == "total_peer" %}
                {{ self.render_column_total_peer(self, context, section, graded_gradeable, index, info, column) }}
            {% elseif column.function == "user_first" %}
                {{ self.render_column_user_first(self, context, section, graded_gradeable, index, info, column) }}
            {% elseif column.function == "user_id" %}
                {{ self.render_column_user_id(self, context, section, graded_gradeable, index, info, column) }}
            {% elseif column.function == "user_id_anon" %}
                {{ self.render_column_user_id_anon(self, context, section, graded_gradeable, index, info, column) }}
            {% elseif column.function == "user_last" %}
                {{ self.render_column_user_last(self, context, section, graded_gradeable, index, info, column) }}
            {% elseif column.function == "viewed_grade" %}
                {{ self.render_column_viewed_grade(self, context, section, graded_gradeable, index, info, column, team_gradeable_view_history) }}
            {% endif %}
        {% endfor %}
    </tr>
{% endmacro %}

{% macro render_teamless_user(self, context, section, user, new_team_onclick, index, columns) %}
    <tr>
        {% for column in columns %}
            {#
                This is basically a switch statement. It calls the function we need for
                the current column and that will give us the table cell.
            #}
            {% if column.function == "active_version" %}
                <td></td>
            {% elseif column.function == "autograding" %}
                <td></td>
            {% elseif column.function == "autograding_peer" %}
                <td></td>
            {% elseif column.function == "graded_questions" %}
                <td></td>
            {% elseif column.function == "grading" %}
                <td><b><i>No Team</i></b></td>
            {% elseif column.function == "grading_peer" %}
                <td></td>
            {% elseif column.function == "index" %}
                {{ self.render_column_index(self, context, section, null, index, null, column) }}
            {% elseif column.function == "section" %}
                {{ self.render_column_section(self, context, section, null, index, null, column) }}
            {% elseif column.function == "team_edit" %}
                {{ self.render_column_team_edit(self, context, section, null, index, new_team_onclick, column) }}
            {% elseif column.function == "team_id" %}
                <td></td>
            {% elseif column.function == "team_members" %}
                <td>
                    {{ user.getDisplayedFirstName() }}
                    {{ user.getDisplayedLastName() }}
                </td>
            {% elseif column.function == "total" %}
                <td></td>
            {% elseif column.function == "total_peer" %}
                <td></td>
            {% elseif column.function == "user_first" %}
                <td></td>
            {% elseif column.function == "user_id" %}
                <td></td>
            {% elseif column.function == "user_id_anon" %}
                <td></td>
            {% elseif column.function == "user_last" %}
                <td></td>
            {% elseif column.function == "viewed_grade" %}
                <td></td>
            {% endif %}
        {% endfor %}
    </tr>
{% endmacro %}

{# Column functions #}

{# Active Version #}
{% macro render_column_active_version(self, context, section, graded_gradeable, index, info, column) %}
    {% if graded_gradeable.getAutoGradedGradeable().getHighestVersion() == 0 %}
        <td></td>
    {% elseif graded_gradeable.getAutoGradedGradeable().getActiveVersion() == graded_gradeable.getAutoGradedGradeable().getHighestVersion() %}
        <td>{{ graded_gradeable.getAutoGradedGradeable().getActiveVersion() }}</td>
    {% else %}
        <td>{{ graded_gradeable.getAutoGradedGradeable().getActiveVersion() }} / {{ graded_gradeable.getAutoGradedGradeable().getHighestVersion() }}</td>
    {% endif %}
{% endmacro %}

{# Autograding #}
{% macro render_column_autograding(self, context, section, graded_gradeable, index, info, column) %}
    <td>
        {% if graded_gradeable.getAutoGradedGradeable().getHighestVersion() != 0 %}
            {% if graded_gradeable.getAutoGradedGradeable().getActiveVersion() != 0  %}
                {{ graded_gradeable.getAutoGradedGradeable().getTotalPoints() }} / {{ graded_gradeable.getGradeable().getAutogradingConfig().getTotalNonExtraCredit() }}
            {% endif %}
        {% endif %}
    </td>
{% endmacro %}

{# Autograding (peer) #}
{% macro render_column_autograding_peer(self, context, section, graded_gradeable, index, info, column) %}
    <td>
        {% if graded_gradeable.getAutoGradedGradeable().getHighestVersion() != 0 %}
            {{ graded_gradeable.getAutoGradedGradeable().getActiveVersionInstance().getNonHiddenPoints() }} / {{ graded_gradeable.getGradeable.getAutogradingConfig().getTotalNonHiddenNonExtraCredit() }}
        {% endif %}
    </td>
{% endmacro %}

{# Graded Questions #}
{% macro render_column_graded_questions(self, context, section, graded_gradeable, index, info, column) %}
    <td class="td-graded-questions">
        {%- for index in 0..info.graded_groups|length-1 -%}
            {%  if graded_gradeable.getAutoGradedGradeable().getActiveVersion() == 0  %}
                <i title="{{graded_gradeable.getGradeable().getComponents()[index].getTitle()}}" class="fas fa-circle grader-NULL"></i>
            {% else %}
                <i title="{{graded_gradeable.getGradeable().getComponents()[index].getTitle()}}" class="fas fa-circle grader-{{- info.graded_groups[index] -}}"></i>
            {% endif %}
        {%- endfor -%}
    </td>
{% endmacro %}

{# Grading Button #}
{% macro render_column_grading(self, context, section, graded_gradeable, index, info, column) %}
    {% if  graded_gradeable.hasOverriddenGrades() %}
        {% set contents = "Overridden" %}
        {% set btn_class = "btn-default" %}
    {% elseif not graded_gradeable.getAutoGradedGradeable().hasSubmission() %}
        {% set contents = "No Submission" %}
        {% set btn_class = "btn-default" %}
    {% elseif graded_gradeable.getAutoGradedGradeable().getActiveVersion() == 0 %}
        {% set contents = "Cancelled Submission" %}
        {% set btn_class = "btn-default" %}
        {% if graded_gradeable.hasActiveRegradeRequest() %}
            {% set contents = contents ~ " + Grade Inquiry" %}
            {% set btn_class = "btn-danger" %}
            {% set badge_count = graded_gradebable.getActiveGradeInquiryCount()  %}
        {%  endif %}
    {% elseif graded_gradeable.getGradeable().isTaGrading() %}
        {% set contents = "Grade" %}
        {% set btn_class = "btn-primary" %}
        {% if graded_gradeable.getOrCreateTaGradedGradeable().anyGrades() %}
            {% if graded_gradeable.getTaGradedGradeable().hasVersionConflict() %}
                {% set contents = "Version Conflict" %}
                {% set btn_class = "btn-primary" %}
            {% elseif not graded_gradeable.isTaGradingComplete() %}
                {% set contents = "Grading Incomplete" %}
            {% elseif graded_gradeable.isTaGradingComplete() %}
                {% set contents = graded_gradeable.getTaGradedGradeable.getTotalScore() ~ " / " ~ graded_gradeable.getGradeable().getTaPoints() %}
                {% set btn_class = "btn-default" %}
            {% endif %}
        {% endif %}
        {% if graded_gradeable.hasActiveRegradeRequest() %}
            {% set contents = contents ~ " + Grade Inquiry" %}
            {% set btn_class = "btn-danger" %}
            {% set badge_count = graded_gradeable.getActiveGradeInquiryCount() %}
        {%  endif %}
    {% else %}
        {% set contents = "View" %}
        {% set btn_class = "btn-primary" %}
    {% endif %}
    <td>
        <a class="btn {{ btn_class }}" href="{{ context.grade_url }}?who_id={{ graded_gradeable.getSubmitter().getId() }}&sort={{ context.sort }}&direction={{ context.direction }}">
            {{ contents }}
            {% if badge_count > 0 and graded_gradeable.getGradeable().isGradeInquiryPerComponentAllowed()%}
                <span class="notification-badge">{{ badge_count }}</span>
            {% endif %}
        </a>
    </td>
{% endmacro %}

{# Grading Button (peer) TODO: fix this for peer grading support
{% macro render_column_grading_peer(self, context, section, gradeable, index, info, column) %}
    {# todo: test this (no way to create peer assignments currently)
    {% set components = gradeable.getComponentsGradedBy(core.getUser().getId()) %}
    {% if components|length == 0 %}
        {% set contents = "Grade" %}
        {% set btn_class = "btn-primary" %}
    {% else %}
        {% set score = 0 %}
        {% for component in components %}
            {% set score = score + component.getScore() %}
        {% endfor %}

        {% set contents = score ~ " / " ~ gradeable.getTotalPeerGradingPoints() %}
        {% set btn_class = "btn-default" %}
    {% endif %}
    <td>
        {% if gradeable.isTeamAssignment() and gradeable.getTeam() == null %}
            <b><i>No Team</i></b>
        {% else %}
            <a class="btn {{ btn_class }}" href="{{ core.buildUrl({'component': 'grading', 'page': 'electronic', 'action': 'grade', 'gradeable_id': gradeable.getId(), 'who_id': gradeable.getUser().getAnonId(), 'sort': context.sort, 'direction': context.direction}) }}">
                {{ contents }}
            </a>
        {% endif %}
    </td>
{% endmacro %}
#}

{# Index #}
{% macro render_column_index(self, context, section, graded_gradeable, index, info, column) %}
    <td>{{ index }}</td>
{% endmacro %}

{# Section Number #}
{% macro render_column_section(self, context, section, graded_gradeable, index, info, column) %}
    <td>{{ section.title }}</td>
{% endmacro %}

{# Edit Team #}
{% macro render_column_team_edit(self, context, section, graded_gradeable, index, team_edit_onclick, column) %}
    <td>
        <a onclick="{{ team_edit_onclick }}" aria-label="Edit Team"><i class="fas fa-pencil-alt"></i></a>
    </td>
{% endmacro %}

{# Team ID #}
{% macro render_column_team_id(self, context, section, graded_gradeable, index, info, column) %}
    <td>
        {% if graded_gradeable.getGradeable().isTeamAssignment() %}
            {{ graded_gradeable.getSubmitter.getId() }}
        {% endif %}
    </td>
{% endmacro %}

{# Team Members #}
{% macro render_column_team_members(self, context, section, graded_gradeable, index, info, column) %}
    <td>
        {% if not graded_gradeable.getGradeable().isTeamAssignment() %}
            {{ graded_gradeable.getSubmitter().getUser().getDisplayedFirstName() }}
            {{ graded_gradeable.getSubmitter().getUser().getDisplayedLastName() }}
        {% else %}
            {%- for member in graded_gradeable.getSubmitter().getTeam().getMemberUsers() -%}
                {%- if loop.index != 1 -%}, {% endif -%}
                {{- member.getDisplayedFirstName() }}
                {{ member.getDisplayedLastName() -}}
            {%- endfor -%}
        {% endif %}
    <div style="font-style: italic; display: inline; color: grey;">
        {% if graded_gradeable.getGradeable().isTeamAssignment() %}
            {%- for member in graded_gradeable.getSubmitter().getTeam().getInvitedUsers() -%}
                {%- if loop.index == 1 -%}Pending: {% endif -%}
                {%- if loop.index != 1 -%}, {% endif -%}
                {{- member.getDisplayedFirstName() }}
                {{ member.getDisplayedLastName() -}}
            {%- endfor -%}
        {% endif %}
    </div>
    </td>
{% endmacro %}

{# Total Point Summary #}
{% macro render_column_total(self, context, section, graded_gradeable, index, info, column) %}
    {% set box_background = (graded_gradeable.getAutoGradedGradeable().getActiveVersionInstance().getDaysLate() > graded_gradeable.getGradeable().getLateDays()) ? "late-box" : "" %}

    {# todo: this is a lot of math for the view, we should move this into the Gradeable model #}

    {# Always get autograder points #}
    {% set graded = graded_gradeable.getAutoGradedGradeable().getActiveVersionInstance().getTotalPoints() %}
    {# If they have a valid, graded submission then they get points from it #}
    {% if graded_gradeable.getAutoGradedGradeable().hasSubmission() and graded_gradeable.getAutoGradedGradeable().getActiveVersion() != 0 and graded_gradeable.isTaGradingComplete() and not graded_gradeable.getOrCreateTaGradedGradeable().hasVersionConflict() %}
        {% set graded = graded + graded_gradeable.getOrCreateTaGradedGradeable().getTotalScore() %}
    {% endif %}
    {# Negative grades are not possible #}
    {% if graded < 0 %}
        {% set graded = 0 %}
    {% endif %}

    {% set total_possible = graded_gradeable.getGradeable().getAutoGradingConfig().getTotalNonExtraCredit() + graded_gradeable.getGradeable().getTaPoints() %}
    <td>
        {% if not graded_gradeable.getOrCreateTaGradedGradeable().hasVersionConflict() and graded_gradeable.isTaGradingComplete() %}
                <div class="{{ box_background }}" style="white-space: nowrap;">{{ graded }} / {{ total_possible }}</div>
        {% endif %}
    </td>
{% endmacro %}

{# Total Point Summary (peer)
{% macro render_column_total_peer(self, context, section, graded_gradeable, index, info, column) %}
    {# todo: test this (no way to create peer assignments currently)
    {# todo: this is a lot of math for the view, we should move this into the Gradeable model
    {% set graded = gradeable.getGradedNonHiddenPoints() %}
    {% set components = gradeable.getComponentsGradedBy(core.getUser().getId()) %}
    {% for component in components %}
        {# getScore is only the custom "mark" need to write a getTotalComponentScore and also make it clear or change name of Score
        {% set graded = graded + component.getScore() %}
    {% endfor %}
    {#
        instead of autograding_score it should be total autograding possible
        I don't think total_peer_grading_points ever gets set...it should be set in the gradeable constructor

    {% set total_possible = gradeable.getGradedNonHiddenPoints() + gradeable.getTotalPeerGradingPoints() %}
    <td>
        {% if gradeable.validateVersions() %}
            <div>{{ graded }} / {{ total_possible }}</div>
        {% endif %}
    </td>
{% endmacro %}
#}

{# User ID #}
{% macro render_column_user_id(self, context, section, graded_gradeable, index, info, column) %}
    <td>
        {% if not graded_gradeable.getGradeable().isTeamAssignment() %}
            {{ graded_gradeable.getSubmitter.getId() }}
        {% endif %}
    </td>
{% endmacro %}

{# Anonymous ID #}
{% macro render_column_user_id_anon(self, context, section, graded_gradeable, index, info, column) %}
    <td>{{ graded_gradeable.getSubmitter().getUser().getAnonId() }}</td>
{% endmacro %}

{# First Name #}
{% macro render_column_user_first(self, context, section, graded_gradeable, index, info, column) %}
    <td>{{ graded_gradeable.getSubmitter().getUser().getDisplayedFirstName() }}</td>
{% endmacro %}

{# Last Name #}
{% macro render_column_user_last(self, context, section, graded_gradeable, index, info, column) %}
    <td>{{ graded_gradeable.getSubmitter().getUser().getDisplayedLastName() }}</td>
{% endmacro %}

{# Viewed Grade #}
{% macro render_column_viewed_grade(self, context, section, graded_gradeable, index, info, column, team_gradeable_view_history) %}
    {% if graded_gradeable.getAutoGradedGradeable().getActiveVersion() > 0 %}
        {% if graded_gradeable.getSubmitter().isTeam() %}
            {% if team_gradeable_view_history[graded_gradeable.getSubmitter().getId()]['hover_string'] == '' %}
                <td class="viewed-red-x" title="">&#10008;</td>
            {% else %}
                <td class="td-viewed-grade" id="team-check-marks" title="{{ team_gradeable_view_history[graded_gradeable.getSubmitter().getId()]['hover_string'] }}">
                    {% for user_id,time in team_gradeable_view_history[graded_gradeable.getSubmitter().getId()] if user_id != 'hover_string' %}
				        {% if time == null %}
                            <i class="viewed-white-check">&#x2714;</i>
                        {% else %}
                            <i class="viewed-green-check">&#x2714;</i>
                        {% endif %}
                    {% endfor %}
                </td>
            {% endif %}
        {% else %}
            {% if graded_gradeable.getOrCreateTaGradedGradeable().getUserViewedDate() == null %}
                <td class="viewed-red-x" title="">&#10008;</td>
            {% else %}
                <td class="viewed-green-check" title="Last Viewed: {{ graded_gradeable.getOrCreateTaGradedGradeable().getUserViewedDate()|date }}">&#x2714;</td>
            {% endif %}
        {% endif %}
    {% else %}
        <td></td>
    {% endif %}
{% endmacro %}

{# /Column functions #}
